<?php

namespace Rbac\Controller;

use Think\Controller;
use Org\Util\Rbac;
use Fn;

/**
 * Description of PublicController
 * 公共的控制器
 * @author Jonas_yang
 */
class PublicController extends Controller {

    /**
     * 登录页面
     */
    public function login() {
        $username = I("post.user_name");
        $password = I("post.user_pwd");
        $loginType = I("post.login_type");
        if ($loginType == "student") {
            $student = M("info_student")->where(array("is_id" => $username, "is_password" => $password))->find();
            if (empty($student)) {
                $resultData["success"] = false;
                $resultData["msg"] = "帐号不存在或密码错误！";
                $this->assign("loginmsg",$resultData["msg"]);
                $this->display("Index/index");
                exit;
            } else {
                $_SESSION[C('USER_AUTH_KEY')] = $student['is_id'];
                //获取学部信息
                $discipline = M("info_discipline")->field("id_name")->find($student["id_id"]);
                $infoClass = M("info_class")->field("icl_number")->find($student["icl_id"]);
                $student["is_login_count"] = $student["is_login_count"]+1;
                M("info_student")->where(array("is_id"=>$username))
                                 ->save(array("is_login_count"=>$student["is_login_count"]));
                $student["id_name"] = $discipline["id_name"];
                $student["icl_number"] = $infoClass["icl_number"];
                if($password=="123456"){
                    $student["is_update_passwd"] = false;
                }else{
                    $student["is_update_passwd"] = true;
                }
                $_SESSION["student_info"] = $student;
               
                $this->display("Student/profile");
                exit;
            }
        }
        $userModel = M("RbacUser");
        $map["account"] = $username;
        $map["status"] = array('gt', 0);
        //$user = $userModel->where($map)->find();
        $authInfo = Rbac::authenticate($map);

        if (empty($authInfo)) {
            $resultData["success"] = false;
            $resultData["msg"] = "帐号不存在或已禁用！";
            $this->assign("loginmsg",$resultData["msg"]);
            $this->display("Index/index");
            exit;
        } else {
            if ($authInfo['password'] != md5($password)) {
                $resultData["success"] = false;
                $resultData["msg"] = "密码错误！";
                $this->assign("loginmsg",$resultData["msg"]);
                $this->display("Index/index");
                exit;
            }
            $_SESSION[C('USER_AUTH_KEY')] = $authInfo['id'];
            $_SESSION["login_type"] = $loginType;
            $_SESSION['email'] = $authInfo['email'];
            $_SESSION['loginUserName'] = $authInfo['nickname'];
            $_SESSION['lastLoginTime'] = $authInfo['last_login_time'];
            $_SESSION['login_count'] = $authInfo['login_count'];
            if ($authInfo['account'] == 'admin') {
                $_SESSION['administrator'] = true;
            }
            //保存登录信息
            $ip = get_client_ip();
            $time = time();
            $data = array();
            $data['id'] = $authInfo['id'];
            $data['last_login_time'] = $time;
            $data['login_count'] = array('exp', 'login_count+1');
            $data['last_login_ip'] = $ip;
            $userModel->save($data);
            // 缓存访问权限
            Rbac::saveAccessList();
            //$resultData["success"] = true;
            //$resultData["msg"] = "登录成功";
            //$this->ajaxReturn($resultData);
            $this->display("Index/manager");
        }
    }

    /**
     * 退出登录
     */
    public function loginout() {
        if (isset($_SESSION[C('USER_AUTH_KEY')])) {
            unset($_SESSION[C('USER_AUTH_KEY')]);
            unset($_SESSION);
            session_destroy();
        }
        $this->display("Index/index");
    }

    /**
     * 获取用户组信息
     */
    public function groupList() {
        $groupList = M("RbacRole")->field("id,name")->select();
        $this->ajaxReturn($groupList);
    }

    public function getSizeByType() {
        $typeId = $_GET["type_id"];
        $sizeList = M("product_type_size")->field("size_id,size_name")
            ->where(array("type_id" => $typeId))
            ->select();
        if (empty($sizeList)) {
            $this->ajaxReturn(array("success" => false, "data" => ""));
        } else {
            $this->ajaxReturn(array("success" => true, "data" => $sizeList));
        }
    }

    /**
     * 输出运行数据
     */
    public function Runtime() {
        //获取学部信息
        $college = D("InfoCollege")->getCollegeList();
        //获取专业信息
        $discipline = D("InfoDiscipline")->getDisciplineList();
        //获取班级信息
        $classinfo = D("InfoClass")->getClassInfoList();
        //获取教师信息
        $teacherInfo = $this->getTeacherInfo();
        $this->assign("teacher", json_encode($teacherInfo));
        $this->assign("college", json_encode($college));
        $this->assign("discipline", json_encode($discipline));
        $this->assign("classinfo", json_encode($classinfo));
        $this->display();
    }

    public function getTeacherInfo(){
        $userIdArr =  M("rbac_role_user")->where(array("role_id"=>3))->select();
        $userModel =  M("rbac_user");
        $rows = array();
        foreach($userIdArr as $userId){
            $user = $userModel->field("id,nickname")->find($userId["user_id"]);
            $rows[] = $user;
        }
        return $rows;
    }

    /**
    *获取所有课程信息
    **/
    public function curriculumInfo(){
        $curriculum = M("info_curriculum")->field("cu_id,cu_name")->select();
        $this->ajaxReturn($curriculum);
    }

}

?>
